#ifndef __GAME_SETTING_H__
#define __GAME_SETTING_H__
#include <string>
#include <vector>

using namespace std;

//是否对文件进行加密
const static bool VALUE_BOOL_IS_FILE_ENCRYPTED = true;
const static int INT_MAX_ROLES = 4;

//文件夹和文件名称
const char PATH_GAME_SETTING[] = "GameSetting";
const char PATH_USER_SETTING[] = "UserSetting";
const char FILE_USER_SETTING[] = "UserSetting.xml";
const char FILE_ROLE_SETTING[] = "RoleSetting.xml";
const char FILE_GAME_SETTING[] = "GameSetting.xml";

//key名字
const char KEY_DES_KEY[]	= {'Y','H','G','A','M','E','W','O','R'};//Des只会用到8个字节作为密码
const char KEY_ROOT[] = "Root";
const char KEY_USER_NAME[] = "UserName";
const char KEY_USERS[]	=	"Users";
const char KEY_USER[]	= "User";
const char KEY_PASSWORD[] = "Password";
const char KEY_IS_REMEMBER_PASSWORD[] = "IsRememberPassword";
const char KEY_ROLES[] = "Roles";
const char KEY_ROLE[] = "Role";
const char KEY_ROLE_NAME[] = "RoleName";
const char KEY_SHOW_PEOPLE_NUMBER[]	=	"ShowPeopleNumber";
const char KEY_IS_MUSIC_ON[]	=	"IsMusicOn";
const char KEY_IS_MUSIC_EFFECT_ON[] = "IsMusicEffectOn";

const char VALUE_BOOL_TRUE[] = "true";
const char VALUE_BOOL_FALSE[] = "false";

/*-----------------des 加密和解密函数----------------*/
//加密，将str字符串加密后写入文件 cipherFile，key是密钥
bool DesEncrypt(const char* content, char* key, const char* cipherFile);
//解密，将cipherFile的内容读取出来，解密后放入dest，dest内部会进行分配，但是外部要负责释放，key是密钥
void DesDecrypt(const char* cipherFile, char* key, char* & dest);

/*
 UserSetting.xml 中的节点：
 <Roles>
 <Role>
 <RoleName>大魔王</RoleName>
 <!--游戏设置-->
 <ShowPeopleNumber>numberAll</ShowPeopleNumber> <!--可选值为numberHide,number15,numberAll-->
 <IsMusicOn>true</IsMusicOn> <!--可选值 true | false -->
 <IsMusicEffectOn>true</IsMusicEffectOn> <!--可选值 true | false -->
 <!--在这里添加更多选项-->
 </Role>
 
 <Role>...</Role>
 ...
 </Roles>
 */
class CRoleSetting
{
public:
	CRoleSetting();
	virtual ~CRoleSetting();
	inline string getRoleName(){return m_roleName;}
	inline void setRoleName(string roleName){m_roleName = roleName;}
	inline bool getIsMusicEffectOn(){return m_isMusicEffectOn;}
	inline void setIsMusicEffectOn(bool isMusicEffectOn){m_isMusicEffectOn = isMusicEffectOn;}
	inline bool getIsMusicOn(){return m_isMusicOn;}
	inline void setIsMusicOn(bool isMusicOn){m_isMusicOn = isMusicOn;}
    
private:
	string m_roleName;
	bool m_isMusicEffectOn;
    bool m_isMusicOn;
};

/*
 UserSetting.xml
 
 <Root>
 <UserName>palmworld></UserName>
 <IsRememberPassword>true</IsRememberPassword>
 <PassWord></PassWord>
 
 <Roles>...</Roles> <!--角色信息-->
 
 </Root>
 */

class CUserSetting
{
public:
	CUserSetting();
	virtual ~CUserSetting();
	bool initSettings(string pathName);
	bool saveSettings();
	inline string getPathName(){return m_pathName;}
	inline string getUserName(){return m_userName;}
	inline bool getIsRememberPassWord(){return m_isRememberPassword;}
	inline string getPassWord() {return m_passWord;}
    
	inline void setUserName(string userName){m_userName = userName;}
	inline void setPassWord(string passWord){m_passWord = passWord;}
	inline void setIsRememberPassWord(bool isRememberPassword){m_isRememberPassword = isRememberPassword;}
    
	inline vector<CRoleSetting*>& getRoles(){return m_roles;}
    
	//根据角色名字获取CRoleSetting，如果不存在则返回NULL
	CRoleSetting* getRoleItemByName(const char* roleName);
	//添加一个roleSetting
	void addRoleItemByName(const char* roleName);
	//删除一个userSetting
	void deleteRoleItemByName(const char* roleName);
	
private:
	string m_pathName;
	string m_userName;
	bool m_isRememberPassword;
	string m_passWord;
	vector<CRoleSetting*> m_roles;
};

/*
 GameSetting.xml
 <Root>
 <Users>
 <User>palmworld</User>
 <...>
 </Users>
 </Root>
 */

class CGameSetting
{
public:
	static CGameSetting* GetSharedInstance();
	static void DestroyInstance();
	static CGameSetting* m_sharedInstance;
	CGameSetting();
	virtual ~CGameSetting();
	//从文件初始化
	bool initSettings();
	//保存到文件
	bool saveSettings();
	//根据用户名字获取CUserSetting，如果不存在则返回NULL
	CUserSetting* getUserItemByName(const char* userName);
	//添加一个userSetting
	void addUserItemByName(const char* userName);
	//删除一个userSetting
	void deleteUserItemByName(const char* userName);
	inline vector<CUserSetting*>& getUsers(){return m_users;}
    
private:
    vector<CUserSetting*> m_users;
    bool m_isMusicOn; //是否开启音乐
	bool m_isMusicEffectOn;//是否开启音效
};

#endif /*__GAME_SETTING_H__*/
